import chhLib.Pachong as Pachong
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
import re

url = "https://so.eastmoney.com/news/s?keyword=%E4%BA%AC%E4%B8%9C"
selector = "span"

class eaestMoney(Pachong.Pachong):
    # def __init__(self, url, selector, mouseXpath,keyXpath=''):
    #     super().__init__(url, selector,mouseXpath,keyXpath)

    def get_html4selenium(self, fag=False,count=0):
        
        s = Service('C:\python312\Scripts\chromedriver.exe')
        options = webdriver.ChromeOptions()
        options.add_argument('--headless')
        options.add_argument('--disable-gpu')
        if fag == True:
            driver = webdriver.Chrome(service=s)
        else:
            driver = webdriver.Chrome(service=s,options=options)
            
        driver.get(self.url)
        html=tmphtml = driver.page_source
        driver.find_element(by='xpath', value=self.mouseXpath).click()
        time.sleep(1)
        
        for i in range(3):
            tmphtml = driver.page_source
            html = html + tmphtml
            footerList = self.parse_html(tmphtml)
            count = len(footerList)
            print(count)
            mouseXpath = self.mouseXpath.replace('5',str(count))
            driver.find_element(by='xpath', value=mouseXpath).click()
            time.sleep(1)

        driver.quit()
        return html
    
if __name__ == '__main__':

    xpath='//*[@id="app"]/div[3]/div[1]/div[4]/div/a[5]'
    parserContent =['<div class="news_item_t".*?><a href=".*?" target="_blank"><em><em>京东</em></em>(.*?)</a></div>',
                    '<span class="news_item_time">(.*?)</span>']
   
    url = "https://so.eastmoney.com/news/s?keyword=%E4%BA%AC%E4%B8%9C"
    selector = "#app > div.main.container > div.c_l > div.c_pager > div > a"
    east = eaestMoney(url, selector, mouseXpath=xpath,parserText=parserContent)
    html = east.get_html4selenium(False)
    contextList = east.parse_html4re(html)
    for i in range(len(contextList)):
        contextList[i] = list(contextList[i])
        contextList[i][0] = re.sub('<.*?>','',contextList[i][0])
        contextList[i][1] = contextList[i][1].split(' ')[0]
    columnsName = ['标题','时间']
    east.save_html(contextList, '东方财富', columnsName)


